package com.week.tiny_video.service.impl;

import com.week.tiny_video.dao.CommentDao;
import com.week.tiny_video.pojo.Comment;
import com.week.tiny_video.pojo.User;
import com.week.tiny_video.pojo.Video;
import com.week.tiny_video.service.CommentService;
import com.week.tiny_video.service.UserService;
import com.week.tiny_video.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentDao commentDao;

    @Autowired
    private UserService userService;

    @Autowired
    private VideoService videoService;
    @Override
    public Comment get(int id) {
        return commentDao.findById(id).orElse(null);
    }

    @Override
    public void add(Comment comment) {
        commentDao.save(comment);

    }

    @Override
    public void update(Comment comment) {
        commentDao.save(comment);
    }

    @Override
    public void delete(int id) {
        commentDao.deleteById(id);
    }

    @Override
    public List<Comment> listByVideo(int vid) {
        Video video = videoService.get(vid);
        Sort sort = new Sort(Sort.Direction.DESC, "id");

        return commentDao.findAllByVideo(video);
    }

    @Override
    public List<Comment> listByUser(int uid) {
        User user = userService.getById(uid);
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        return commentDao.findAllByUser(user);
    }
}
